package presstrack
{
	import flash.net.Responder;
	
	import mx.utils.ObjectUtil;
	
	import org.presstrack.service.*;
	
	[RemoteClass(alias="presstrack.UserClass")]
	
	[Bindable]
	public class UserClass
	{
		public var iUserId:int;
  		public var mNick:String;
  		public var mPass:String;
  		public var mName:String;
  		public var iSection:int;
  		public var arContact:Array;
  		public var iMediumId:int;
  		public var obStatistic:Object;
  		
  		public function UserClass(user:UserClass = null)
        {
            if (user != null)
            {
            	copyFrom(user);
            }
        }
        
        public function copyFrom(user:UserClass):void
        {
            iUserId		= user.iUserId;
            mNick    	= user.mNick;
            mPass    	= user.mPass;
            mName    	= user.mName;
            iSection	= user.iSection;
            arContact	= user.arContact;
            iMediumId	= user.iMediumId;
            obStatistic	= user.obStatistic;
        }
        
        public function update(param		:String, 
        					   value		:String, 
        					   resFunc		:Function 	= null, 
        					   faultFunc	:Function	= null ):void 
        {
			CrudService.crud.srv.call("PressTrack.UpdateUser", new Responder(onResult, onFault), 
									 iUserId, param, value);
				
			function onResult(res:String):void 
			{
				if (res == 'successful') 
				{
					switch (param) {
						case "name" :
							mName = value;
						break;
					} 
					CrudService.arCustomer.refresh();
				}
				else
				{
					faultFunc(res);	
				}			
			}	
			
			function onFault(e:Object):void 
			{
				faultFunc(ObjectUtil.toString(e));
			}		
		}
		
        public function getStatistic(mode		:String, 
        							 year		:String, 
        							 month		:String, 
        							 resFunc	:Function	= null, 
        							 faultFunc	:Function	= null):void
        {
            CrudService.crud.srv.call("PressTrack.GetStaticticForUser", new Responder(onResult, onFault), 
            	iUserId, mode, year, month);	
			
			function onResult(statistic:Object):void 
			{
				if (mode == "by_year")
				{
					obStatistic = statistic;
				}
				else if (mode == "by_month")
				{
					if (statistic.hasOwnProperty("iNumOfOrderForMonth"))
					{
						obStatistic.iNumOfOrderForMonth 	= new int(statistic.iNumOfOrderForMonth);
					}
					else
					{
						obStatistic.iNumOfOrderForMonth 	= new int();
					}
					
					if (statistic.hasOwnProperty("dSumOfCostForMonth"))
					{
						obStatistic.dSumOfCostForMonth 	= new Number(statistic.dSumOfCostForMonth);
					}
					else
					{
						obStatistic.dSumOfCostForMonth 	= new Number();
					}
					
					if (statistic.hasOwnProperty("dSumOfPaymentForMonth"))
					{
						obStatistic.dSumOfPaymentForMonth 	= new Number(statistic.dSumOfPaymentForMonth);
					}
					else
					{
						obStatistic.dSumOfPaymentForMonth 	= new Number();
					}
					
					if (statistic.hasOwnProperty("arCustBySumOfOrder"))
					{
						obStatistic.arCustBySumOfOrder 	= statistic.arCustBySumOfOrder;
					}
					else
					{
						obStatistic.arCustBySumOfOrder 	= new Array();
					}
					
					if (statistic.hasOwnProperty("arCustByNumOfOrder"))
					{
						obStatistic.arCustByNumOfOrder 	= statistic.arCustByNumOfOrder;
					}
					else
					{
						obStatistic.arCustByNumOfOrder 	= new Array();
					}
					
					if (statistic.hasOwnProperty("arCustByPaymentAndDebt"))
					{
						obStatistic.arCustByPaymentAndDebt 	= statistic.arCustByPaymentAndDebt;
					}
					else
					{
						obStatistic.arCustByPaymentAndDebt 	= new Array();
					}
				}
				
				if (resFunc != null)
				{
					resFunc();
				}				
			}
			
			function onFault(e:Object):void 
			{
				faultFunc(ObjectUtil.toString(e));
			}
        }        
	}
}